package com.vm.adapter;

import java.util.List;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.vm.activity.BitmapCache;
import com.vm.commons.StringUtil;
import com.vm.commons.VMConst;
import com.vm.db.UpgradeSoftDao;
import com.vm.model.AppInfo;
import com.vm.model.Soft;
import com.vm.model.UpgradeSoft;
import com.wwt.app.R;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

/**
 * 
 * @ProjectName: [WeiMai]
 * @Author: [kent.wang]
 * @CreateDate: [2015年2月2日 上午10:32:21]
 * @Version: [v1.0]
 * 
 */
public class UninstallAdapter extends BaseAdapter {

	private Context mContext;
	private List<UpgradeSoft> listSofts;

	/**
	 * 
	 */
	public UninstallAdapter(Context context, List<UpgradeSoft> lists) {
		this.mContext = context;
		this.listSofts = lists;

		RequestQueue requestQueue = Volley.newRequestQueue(context);
	}

	@Override
	public int getCount() {
		return listSofts.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder = null;
		if (convertView == null) {

			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.software_uninstall_item, null);
			viewHolder = new ViewHolder();

			/**
			 * 获取各个需要显示属性的控件
			 */
			viewHolder.tv_softName = (TextView) convertView
					.findViewById(R.id.tv_softName);
			viewHolder.iv_softImage = (ImageView) convertView
					.findViewById(R.id.iv_softImage);
			viewHolder.tv_softSize = (TextView) convertView
					.findViewById(R.id.tv_softSize);
			viewHolder.tv_now_version_number = (TextView) convertView
					.findViewById(R.id.tv_now_version_number);
			viewHolder.btn_uninstall = (Button) convertView
					.findViewById(R.id.btn_uninstall);
			viewHolder.btn_uninstall.setOnClickListener(btn_uninstallListener);
			// viewHolder.rb_star = (RatingBar) convertView
			// .findViewById(R.id.rb_star);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		UpgradeSoft appInfo = listSofts.get(position);

		viewHolder.tv_softName.setTag(appInfo.getName());
		viewHolder.btn_uninstall.setTag(appInfo.getPackageName());
		// 名称

		if (appInfo.getName() != null) {
			// String softName = StringUtil.getShotName(soft.getName());
			viewHolder.tv_softName.setText(appInfo.getName());
		}
		
		if (appInfo.getCurrentVersion() != null) {
			viewHolder.tv_now_version_number.setText(appInfo.getCurrentVersion());
		}

		if (appInfo.getPackageName() != null) {
			try {
				viewHolder.iv_softImage.setImageDrawable(mContext.getPackageManager().getApplicationIcon(appInfo.getPackageName()));
			} catch (NameNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}	
		return convertView;
	}

	OnClickListener btn_uninstallListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			String packageName = v.getTag().toString();
			Uri packageURI = Uri.parse("package:" + packageName);
			Intent uninstallIntent = new Intent(Intent.ACTION_DELETE,
					packageURI);
			v.getContext().startActivity(uninstallIntent);
			
			//执行本地数据库删除
			UpgradeSoftDao dao = new UpgradeSoftDao(mContext);
			dao.delete(packageName);
		}
	};

	static class ViewHolder {
		TextView tv_softName;
		ImageView iv_softImage;
		TextView tv_softSize;
		TextView tv_now_version_number;
		Button btn_uninstall;
		// RatingBar rb_star;
	}

}
